//
//  ApiClient.m
//  weappios
//
//  Created by chensentao on 2018/8/3.
//  Copyright © 2018年 XiGuangLu. All rights reserved.
//

#import "ApiClient.h"
#import "NSMutableDictionary+AllType.h"
#import "HttpHelper.h"
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import "AFNetworking.h"
#import "AppDelegate.h"
#import "AppConfig.h"
#import "AppContext.h"
#import "NSString+URL.h"

@interface ApiClient()
{
    
}
@property(nonatomic,strong) HttpHelper *http;
@end

@implementation ApiClient

+(ApiClient*)sharedApi
{
    static dispatch_once_t token;
    static ApiClient* api;
    dispatch_once(&token, ^{
        api=[[ApiClient alloc] init];
    });
    return api;
    
}

-(instancetype)init
{
    self = [super init]; //用于初始化父类
    if (self) {
        _http=[[HttpHelper alloc] init];
    }
    return self;
}

-(void)personal:(NSDictionary*)dict cb:(CallbackResult)callback
{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_PERSONAL key:@"m"];
    [tmp putStr:[dict getStr:@"uid"] key:@"uid"];
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result= [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)send_code:(NSDictionary*)dict cb:(CallbackResult)callback
{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_SEND_CODE key:@"m"];
    [tmp putStr:[dict getStr:@"phone"] key:@"phone"];
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result= [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)check_code:(NSDictionary*)dict cb:(CallbackResult)callback
{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_CHECK_CODE key:@"m"];
    [tmp putStr:[dict getStr:@"phone"] key:@"phone"];
    [tmp putStr:[dict getStr:@"code"] key:@"code"];
    NSString* channel=[dict getStr:@"channel"];
    if(channel){
        [tmp putStr:channel key:@"channel"];
    }
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)ios_check_code:(NSDictionary*)dict cb:(CallbackResult)callback
{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_IOS_CHECK_CODE key:@"m"];
    [tmp putStr:[dict getStr:@"phone"] key:@"phone"];
    [tmp putStr:[dict getStr:@"code"] key:@"code"];
    NSString* channel=[dict getStr:@"channel"];
    if(channel){
        [tmp putStr:channel key:@"channel"];
    }
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)jump_reward:(NSDictionary*)dict cb:(CallbackResult)callback
{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_JUMP_REWARD key:@"m"];
    [tmp putStr:[dict getStr:@"uid"] key:@"uid"];
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)play:(NSDictionary*)dict cb:(CallbackResult)callback
{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_PLAY key:@"m"];
    [tmp putStr:[dict getStr:@"uid"] key:@"uid"];
    [tmp putStr:[dict getStr:@"weappid"] key:@"weappid"];
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)step:(CallbackResult)callback
{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_STEP key:@"m"];
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

- (void)getui_cid:(NSDictionary*)dict cd:(CallbackResult)callback {
    
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_GETUI_CID key:@"m"];
    [tmp putStr:[dict getStr:@"uid"] key:@"uid"];
    [tmp putStr:[dict getStr:@"cid"] key:@"cid"];
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)jumpAuth:(NSDictionary *)dict cb:(CallbackResult)callback{
    NSMutableDictionary* tmp=[NSMutableDictionary dictionary];
    [tmp putStr:MODULE_jumpAuth key:@"m"];
    [tmp putStr:[dict getStr:@"uid"] key:@"uid"];
    [_http post:API_URL prams:tmp func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)share:(NSDictionary *)dict cb:(CallbackResult)callback{
    NSMutableDictionary * tep = [NSMutableDictionary dictionary];
    [tep putStr:MODULE_share key:@"m"];
    [tep putStr:[dict getStr:@"uid"] key:@"uid"];
    [tep putStr:[dict getStr:@"aid"] key:@"aid"];
    [tep putStr:[dict getStr:@"ua"] key:@"ua"];
    [_http post:API_SHARE prams:tep func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}

-(void)pay:(NSDictionary *)dict cb:(CallbackResult)callback
{
    NSMutableDictionary * tep = [NSMutableDictionary dictionary];
    [tep putStr:MODULE_pay key:@"m"];
    [tep putStr:[dict getStr:@"uid"] key:@"uid"];
    [tep putStr:[dict getStr:@"count"] key:@"spc"];
    [tep putStr:@"ios" key:@"ua"];
    [_http post:API_PAYURL prams:tep func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}
    
- (void)yiqishike_userp:(NSDictionary *)dict cb:(CallbackResult)callback {
    
    JKEncrypt * en = [[JKEncrypt alloc]init];
    NSString *idfa = [AppConfig getAppIDFA];
    NSString *app_Version = [AppConfig getAppVersion];
    NSString *idfv = [AppConfig getAppIDFV];
    NSString *bundleid = [AppConfig getAppBundleId];
    NSString *status = [AppConfig getNetWorkStates];
    NSString *imsi = [AppConfig getIMSI];
    NSString *model = [AppConfig getDeviceModel];
    NSString *device_Version = [AppConfig getDeviceVersion];
    NSString *janlbreak = [AppConfig isJailBreak];
    NSMutableDictionary * tep = [NSMutableDictionary dictionary];
    [tep putStr:[en Base64AfterEncryptStr:[dict getStr:@"udid"]] key:@"fsd"];
    [tep putStr:[en DES3EncryptStr:idfa] key:@"ukd"];
    //iPhone_&iPhone9,1_&IOS_&11.4.1
    //设备+设备型号+IOS+系统版本号
    NSString *dtt = [NSString stringWithFormat:@"iPhone_&%@_&IOS_&%@",model,device_Version];
    [tep putStr:[en Base64AfterEncryptStr:dtt] key:@"dtt"];
    [tep putStr:[en DES3EncryptStr:app_Version] key:@"sr"];
    [tep putStr:[en DES3EncryptStr:janlbreak] key:@"ss"];
    [tep putStr:[en DES3EncryptStr:imsi] key:@"kk1"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"dr1"];
    [tep putStr:[en DES3EncryptStr:status] key:@"ur"];
    [tep putStr:[en DES3EncryptStr:idfv] key:@"crm"];
    [tep putStr:[en DES3EncryptStr:bundleid] key:@"dot"];
    [tep putStr:[en Base64AfterEncryptStr:@""] key:@"cor"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"gte"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"qx9"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"sid"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"bsid"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"ph"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"co"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"uks"];
    [self AFNRequestUrl:@"/?m=sdk&a=userp" Parameters:tep Encrypt:en cb:callback];
//    [_http post:[API_ZHUSHOU stringByAppendingString:@"/?m=sdk&a=userp"] prams:tep func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        NSString *str =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//        NSString *decEncryptStr = [en Base64AfterDecEncryptStr:str];
//        NSData *resonData = [decEncryptStr dataUsingEncoding:NSUTF8StringEncoding];
//        NSDictionary* result=nil;
//        if(resonData){
//            result=[NSJSONSerialization JSONObjectWithData:resonData options:0 error:nil];
//        }
//        if(callback){
//            callback(result,connectionError);
//        }
//    }];
}
    
- (void)yiqishike_send_active:(NSDictionary *)dict cb:(CallbackResult)callback {
    
    JKEncrypt * en = [[JKEncrypt alloc]init];
    NSString *idfa = [AppConfig getAppIDFA];
    NSString *app_Version = [AppConfig getAppVersion];
    NSString *idfv = [AppConfig getAppIDFV];
    NSString *bundleid = [AppConfig getAppBundleId];
    NSString *status = [AppConfig getNetWorkStates];
    NSString *imsi = [AppConfig getIMSI];
    NSString *model = [AppConfig getDeviceModel];
    NSString *device_Version = [AppConfig getDeviceVersion];
    NSString *janlbreak = [AppConfig isJailBreak];
    NSMutableDictionary * tep = [NSMutableDictionary dictionary];
    [tep putStr:[en Base64AfterEncryptStr:[dict getStr:@"udid"]] key:@"ois"];
    [tep putStr:[en DES3EncryptStr:idfa] key:@"u89"];
    NSString *plo = [NSString stringWithFormat:@"iPhone_&%@_&IOS_&%@",model,device_Version];
    [tep putStr:[en Base64AfterEncryptStr:plo] key:@"plo"];
    [tep putStr:[en DES3EncryptStr:app_Version] key:@"cvd"];
    [tep putStr:[en DES3EncryptStr:[dict getStr:@"uid"]] key:@"kk"];
    [tep putStr:[en DES3EncryptStr:[dict getStr:@"taskid"]] key:@"rw"];
    [tep putStr:[en DES3EncryptStr:janlbreak] key:@"nn"];
    [tep putStr:[en DES3EncryptStr:imsi] key:@"mm"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"nnx"];
    [tep putStr:[en DES3EncryptStr:status] key:@"ttd"];
    [tep putStr:[en DES3EncryptStr:idfv] key:@"oou"];
    [tep putStr:[en DES3EncryptStr:bundleid] key:@"eft"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"clr"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"fq"];
    [tep putStr:[en Base64AfterEncryptStr:@""] key:@"fs"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"yzr"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"ddl"];
    [self AFNRequestUrl:@"/?m=sdk&a=send_active" Parameters:tep Encrypt:en cb:callback];
}
    
- (void)yiqishike_logd:(NSDictionary *)dict cb:(CallbackResult)callback {
    
    JKEncrypt * en = [[JKEncrypt alloc]init];
    NSString *idfa = [AppConfig getAppIDFA];
    NSString *app_Version = [AppConfig getAppVersion];
    NSString *idfv = [AppConfig getAppIDFV];
//    NSString *bundleid = [AppConfig getAppBundleId];
    NSString *status = [AppConfig getNetWorkStates];
    NSString *imsi = [AppConfig getIMSI];
    NSString *model = [AppConfig getDeviceModel];
    NSString *device_Version = [AppConfig getDeviceVersion];
    NSString *janlbreak = [AppConfig isJailBreak];
    NSMutableDictionary * tep = [NSMutableDictionary dictionary];
    [tep putStr:[en Base64AfterEncryptStr:[dict getStr:@"udid"]] key:@"qw"];
    [tep putStr:[en DES3EncryptStr:idfa] key:@"as"];
    NSString *er = [NSString stringWithFormat:@"iPhone_&%@_&IOS_&%@",model,device_Version];
    [tep putStr:[en Base64AfterEncryptStr:er] key:@"er"];
    [tep putStr:[en DES3EncryptStr:app_Version] key:@"df"];
    [tep putStr:[en DES3EncryptStr:[dict getStr:@"uid"]] key:@"cv"];
    [tep putStr:[en DES3EncryptStr:[dict getStr:@"taskid"]] key:@"ty"];
    [tep putStr:[en DES3EncryptStr:[dict getStr:@"keyword"]] key:@"kw"];
    [tep putStr:[en DES3EncryptStr:janlbreak] key:@"gh"];
    [tep putStr:[en DES3EncryptStr:imsi] key:@"ui"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"jk"];
    [tep putStr:[en DES3EncryptStr:status] key:@"mm"];
    [tep putStr:[en DES3EncryptStr:idfv] key:@"op"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"ll"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"dd"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"clr"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"uj"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"ik"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"yzr"];
    [tep putStr:[en DES3EncryptStr:@""] key:@"ol"];
    [self AFNRequestUrl:@"/?m=sdk&a=logd" Parameters:tep Encrypt:en cb:callback];
}
    
- (void)yiqishike_phone:(NSDictionary *)dict cb:(CallbackResult)callback {
    
    JKEncrypt * en = [[JKEncrypt alloc]init];
    NSMutableDictionary * tep = [NSMutableDictionary dictionary];
    [tep putStr:@"" key:@"phone"];//手机号码
    [tep putStr:@"" key:@"ol"];//签名
    [self AFNRequestUrl:@"/?m=sdk&a=phone" Parameters:tep Encrypt:en cb:callback];
}

- (void)AFNRequestUrl:(NSString *)url Parameters:(NSDictionary *)param Encrypt:(JKEncrypt *)encrypt cb:(CallbackResult)callback{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:[[AppContext sharedContext].api_zhushou stringByAppendingString:url] parameters:param headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"responseObject===%@",responseObject);
//        NSString *str =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSString *decEncryptStr = [encrypt Base64AfterDecEncryptStr:responseObject];
        NSData *resonData = [decEncryptStr dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary* result=nil;
        if(resonData){
            result=[NSJSONSerialization JSONObjectWithData:resonData options:0 error:nil];
        }
        if(callback){
            callback(result,nil);
        }
        dispatch_semaphore_signal(semaphore);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error===%@",error);
    }];
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

}

- (void)getSchemescb:(CallbackResult)callback
{
    [_http get:@"https://testweapp.whwangdoudou.cn/assets/uploads/iosbw.json" prams:nil func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
//        if(data){
//            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//        }
        NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        result = @{@"schemes":jsonStr};
        if(callback){
            callback(result,connectionError);
        }
    }];
}

- (void)getChannelcb:(CallbackResult)callback
{
    [_http get:@"https://testweapp.whwangdoudou.cn/zhushou.json" prams:nil func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary *result = nil;
        NSArray *channelArr = nil;
        if(data){
            channelArr=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
//        NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        result = @{@"result":channelArr};
        if(callback){
            callback(result,connectionError);
        }
    }];
}

- (void)postRewardParameters:(NSDictionary *)param cb:(CallbackResult)callback {
    
    NSMutableDictionary * tep = [NSMutableDictionary dictionary];
//    [tep putStr:MODULE_pay key:@"m"];
    [tep putInt:[param getInt:@"reward_amount"] key:@"reward_amount"];
    [tep putStr:[param getStr:@"reward_name"] key:@"reward_name"];
    [tep putStr:[param getStr:@"user_id"] key:@"user_id"];
    [tep putStr:[param getStr:@"extra"] key:@"extra"];
    [_http post:@"https://weapp.whwangdoudou.cn/vreward_ios.php" prams:tep func:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary* result=nil;
        if(data){
            result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(callback){
            callback(result,connectionError);
        }
    }];
}


@end
